package com.macro.mall.controller;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dto.PmsItem;
import com.macro.mall.req.PmsItemReq;
import com.macro.mall.service.PmsItemService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Author：Minxiao-Hao
 * @Description: 物品信息管理控制层
 * @name：PmsitemController
 * @Date：2024/11/2 9:57
 */
@Controller
@RequestMapping("/item")
public class PmsItemController {
    @Autowired
    private PmsItemService itemService;

    @Operation(summary = "查询物品信息")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<PmsItem>> list() {
        List<PmsItem> list = itemService.list();
        return CommonResult.success(list);
    }
    @Operation(summary = "添加物品")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult add(@RequestBody @Validated PmsItemReq pmsItemReq) {
        itemService.create(pmsItemReq);
        return CommonResult.successMessage("操作成功");
    }

    @Operation(summary = "删除物品")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public CommonResult delete(@PathVariable Long id) {
         itemService.delete(id);
        return CommonResult.successMessage("操作成功");
    }

    @Operation(summary = "修改物品")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public CommonResult update(@PathVariable Long id,@RequestBody  @Validated PmsItemReq pmsItemReq) {
        itemService.update(id,pmsItemReq);
        return CommonResult.successMessage("操作成功");
    }
    @Operation(summary = "根据id查询对象")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmsItem> add(@PathVariable Long id) {
        PmsItem list = itemService.findById(id);
        return CommonResult.success(list);
    }
    @Operation(summary = "上传图片")
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public String upload(MultipartFile file) {
        String url = itemService.upload(file);
        return url;
    }
}
